﻿using PLCS.Client.IService;

using System.Collections.ObjectModel;

namespace PLCS.Client.Service;

/// <summary>
/// 客户端服务基类
/// </summary>
/// <typeparam name="Output"></typeparam>
/// <typeparam name="Input"></typeparam>
public class BaseService<Output, Input> : IBaseService<Output, Input>
{
    public HttpRestClient restClient = new();

    public virtual async Task CreateAsync(Input input)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"{typeof(Output).Name.ToLower().Replace("dto", "")}", parameter: input);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public virtual async Task DeleteByIdAsync(Guid Id)
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"{typeof(Output).Name.ToLower().Replace("dto", "")}/{Id}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public virtual async Task<IEnumerable<Output>> GetListAsync()
    {
        RequestParam requestParam = new(method: Method.Get, route: $"{typeof(Output).Name.ToLower().Replace("dto", "")}");
        var apiRes = await restClient.ExecuteAsync<ObservableCollection<Output>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return Enumerable.Empty<Output>();
    }

    public virtual async Task UpdateAsync(Input input)
    {
        RequestParam requestParam = new(method: Method.Put, route: $"{typeof(Output).Name.ToLower().Replace("dto", "")}", parameter: input);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }
}